package crp.infra.test.db;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.testcontainers.containers.PostgreSQLContainer;

import javax.inject.Inject;
import java.util.Map;

public class PostgresResource implements QuarkusTestResourceLifecycleManager {
    @Override
    public Map<String, String> start() {
        PostgreSQLContainer<?> db = PostgreSQLContainerProvider.get();
        db.start();
        return Map.of(
                "quarkus.datasource.jdbc.url", db.getJdbcUrl(),
                "quarkus.datasource.username", db.getUsername(),
                "quarkus.datasource.password", db.getPassword()
        );
    }

    @Override
    public void stop() {
        PostgreSQLContainerProvider.get().stop();
    }


    @Override
    public void inject(TestInjector testInjector) {
        testInjector.injectIntoFields(PostgreSQLContainerProvider.get(),
                new TestInjector.AnnotatedAndMatchesType(Inject.class, PostgreSQLContainer.class));
    }
}
